<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: udpServer.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>udpServer.c</h1><a href="udp_server_8c.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* fpont 12/99 */</span>
<a name="l00002"></a>00002 <span class="comment">/* pont.net    */</span>
<a name="l00003"></a>00003 <span class="comment">/* udpServer.c */</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;unistd.h&gt;</span> <span class="comment">/* close() */</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;string.h&gt;</span> <span class="comment">/* memset() */</span>
<a name="l00013"></a>00013 
<a name="l00014"></a><a class="code" href="udp_server_8c.html#aa94354c85f94992aedbe97da4da440b">00014</a> <span class="preprocessor">#define LOCAL_SERVER_PORT 1500</span>
<a name="l00015"></a><a class="code" href="udp_server_8c.html#a24597a54a085c6c2c33b64138f09eff">00015</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_MSG 100</span>
<a name="l00016"></a>00016 <span class="preprocessor"></span>
<a name="l00017"></a><a class="code" href="udp_server_8c.html#0ddf1224851353fc92bfbff6f499fa97">00017</a> <span class="keywordtype">int</span> <a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {
<a name="l00018"></a>00018   
<a name="l00019"></a>00019   <span class="keywordtype">int</span> sd, rc, n, cliLen;
<a name="l00020"></a>00020   <span class="keyword">struct </span>sockaddr_in cliAddr, servAddr;
<a name="l00021"></a>00021   <span class="keywordtype">char</span> msg[<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>];
<a name="l00022"></a>00022 
<a name="l00023"></a>00023   <span class="comment">/* socket creation */</span>
<a name="l00024"></a>00024   sd=socket(AF_INET, SOCK_DGRAM, 0);
<a name="l00025"></a>00025   <span class="keywordflow">if</span>(sd&lt;0) {
<a name="l00026"></a>00026     printf(<span class="stringliteral">"%s: cannot open socket \n"</span>,argv[0]);
<a name="l00027"></a>00027     exit(1);
<a name="l00028"></a>00028   }
<a name="l00029"></a>00029 
<a name="l00030"></a>00030   <span class="comment">/* bind local server port */</span>
<a name="l00031"></a>00031   servAddr.sin_family = AF_INET;
<a name="l00032"></a>00032   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
<a name="l00033"></a>00033   servAddr.sin_port = htons(<a class="code" href="udp_server_8c.html#aa94354c85f94992aedbe97da4da440b">LOCAL_SERVER_PORT</a>);
<a name="l00034"></a>00034   rc = bind (sd, (<span class="keyword">struct</span> sockaddr *) &amp;servAddr,<span class="keyword">sizeof</span>(servAddr));
<a name="l00035"></a>00035   <span class="keywordflow">if</span>(rc&lt;0) {
<a name="l00036"></a>00036     printf(<span class="stringliteral">"%s: cannot bind port number %d \n"</span>, 
<a name="l00037"></a>00037            argv[0], <a class="code" href="udp_server_8c.html#aa94354c85f94992aedbe97da4da440b">LOCAL_SERVER_PORT</a>);
<a name="l00038"></a>00038     exit(1);
<a name="l00039"></a>00039   }
<a name="l00040"></a>00040 
<a name="l00041"></a>00041   printf(<span class="stringliteral">"%s: waiting for data on port UDP %u\n"</span>, 
<a name="l00042"></a>00042            argv[0],<a class="code" href="udp_server_8c.html#aa94354c85f94992aedbe97da4da440b">LOCAL_SERVER_PORT</a>);
<a name="l00043"></a>00043 
<a name="l00044"></a>00044   <span class="comment">/* server infinite loop */</span>
<a name="l00045"></a>00045   <span class="keywordflow">while</span>(1) {
<a name="l00046"></a>00046     
<a name="l00047"></a>00047     <span class="comment">/* init buffer */</span>
<a name="l00048"></a>00048     memset(msg,0x0,<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>);
<a name="l00049"></a>00049 
<a name="l00050"></a>00050 
<a name="l00051"></a>00051     <span class="comment">/* receive message */</span>
<a name="l00052"></a>00052     cliLen = <span class="keyword">sizeof</span>(cliAddr);
<a name="l00053"></a>00053     n = recvfrom(sd, msg, <a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>, 0, 
<a name="l00054"></a>00054                  (<span class="keyword">struct</span> sockaddr *) &amp;cliAddr, &amp;cliLen);
<a name="l00055"></a>00055 
<a name="l00056"></a>00056     <span class="keywordflow">if</span>(n&lt;0) {
<a name="l00057"></a>00057       printf(<span class="stringliteral">"%s: cannot receive data \n"</span>,argv[0]);
<a name="l00058"></a>00058       <span class="keywordflow">continue</span>;
<a name="l00059"></a>00059     }
<a name="l00060"></a>00060       
<a name="l00061"></a>00061     <span class="comment">/* print received message */</span>
<a name="l00062"></a>00062     printf(<span class="stringliteral">"%s: from %s:UDP%u : %s \n"</span>, 
<a name="l00063"></a>00063            argv[0],inet_ntoa(cliAddr.sin_addr),
<a name="l00064"></a>00064            ntohs(cliAddr.sin_port),msg);
<a name="l00065"></a>00065     
<a name="l00066"></a>00066   }<span class="comment">/* end of server infinite loop */</span>
<a name="l00067"></a>00067 
<a name="l00068"></a>00068 <span class="keywordflow">return</span> 0;
<a name="l00069"></a>00069 
<a name="l00070"></a>00070 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:26 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
